package com.aceitunaproject.webclient.client;


import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Displays the login dialog and manages the user login.
 * 
 * @author Federico Bergstein
 */
public class LoginBox {

	
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";
	
	final static DialogBox dialogBox = new DialogBox();
	final static Button closeButton = new Button("Cancel");
	final static Button loginButton = new Button("Login");
	final TextBox userNameTextBox = new TextBox();
	final HTML serverResponseLabel = new HTML();
	private boolean isLoggedIn = false;
	private MainAceitunaClient aceitunaClient;
	public LoginBox(final MainAceitunaClient aceitunaClient) {

		// Create the popup dialog box
		this.aceitunaClient = aceitunaClient;
		dialogBox.setText("Login");
		dialogBox.setAnimationEnabled(true);
		
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		final Label userNameLabel = new Label();
		
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(userNameLabel);
		dialogVPanel.add(userNameTextBox);
		dialogVPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		dialogVPanel.add(loginButton);
		dialogVPanel.add(serverResponseLabel);
		dialogBox.setWidget(dialogVPanel);

		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				dialogBox.hide();
				if(isLoggedIn)
				{
					aceitunaClient.UserLoggedIn(userNameTextBox.getText());
				}
			}
		});
		// Add a handler to the Login button
		loginButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				sendNameToServer();;

			}
		});
		
		dialogBox.center();
	}
	
	/**
	 * Send the name from the nameField to the server and wait for a
	 * response.
	 */
	private  void sendNameToServer() {

		// Then, we send the input to the server.
		loginButton.setEnabled(false);

		//serverResponseLabel.setText("");
		aceitunaClient.getAceitunaService().login(userNameTextBox.getText(), "test",
				new AsyncCallback<String>() {
					@Override
					public void onFailure(Throwable caught) {
						// Show the RPC error message to the user
						dialogBox
								.setText("Remote Procedure Call - Failure");
						serverResponseLabel
								.addStyleName("serverResponseLabelError");
						serverResponseLabel.setHTML(SERVER_ERROR);
						dialogBox.center();
						closeButton.setFocus(true);
					}

					@Override
					public void onSuccess(String result) {
						dialogBox.setText("Remote Procedure Call");
						serverResponseLabel
								.removeStyleName("serverResponseLabelError");
						serverResponseLabel.setHTML("User " +  userNameTextBox.getText() + " sucessfully logged in to Aceituna Server");
						aceitunaClient.setClientId(result);
						dialogBox.center();
						closeButton.setFocus(true);
						loginButton.setVisible(false);
						closeButton.setText("Close");
						isLoggedIn = true;
						userNameTextBox.setEnabled(false);
					}
				});
	}
}


